package com.xebest.web.controller.salesBill;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.web.form.XeVenFinSalesBillDetailForm;
import com.xebest.web.model.base.XePageInfo;
import com.xebest.web.service.XeVenFinSalesBillDetailService;
import com.xebest.web.view.XeVenFinSalesBillDetailView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收款单商品明细
 *
 * @auther wangyp
 */
@RestController
@RequestMapping("xeVenFinSalesBillDetail")
@Scope("prototype")
public class XeVenFinSalesBillDetailController extends BaseController {

    //日志记录类
    private static final Logger log = LoggerFactory.getLogger(XeVenFinSalesBillDetailController.class);

    @Autowired
    private XeVenFinSalesBillDetailService xeVenFinSalesBillDetailService;   //收款单商品明细业务操作


    /**
     * 收款单商品明细列表
     *
     * @auther wangyp
     */
    @RequestMapping("queryXeVenFinSalesBillDetailList")
    public @ResponseBody
    BaseResponse queryXeVenFinSalesBillDetailList(@RequestBody @Validated XeVenFinSalesBillDetailForm form,
                                                  BindingResult result) {
        log.info("收款单明细查询入参" + JSONArray.toJSONString(form));
        //验证参数
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            log.info("收款单商品明细缺少参数：{}, {}",
                    fieldError.getField(),
                    fieldError.getDefaultMessage());
            return sendAjaxError(BASEMessage.PARAM_ERROR, fieldError.getDefaultMessage());
        }
        //拼装参数
        XePageInfo<XeVenFinSalesBillDetailView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //输出结果
        List<XeVenFinSalesBillDetailView> salesBillDetailList = this.xeVenFinSalesBillDetailService.queryXeVenFinSalesBillDetailList(form);
        try {
            xePageInfo = new XePageInfo<XeVenFinSalesBillDetailView>(salesBillDetailList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("salesBillDetailList", salesBillDetailList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("收款单商品明细查询List异常：{}", e);
            return sendAjaxError();
        }
    }

}
